package com.itmk.config.web.sys_menu.entity;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class MakeMenuTree {
    public static List<SysMenu> makeTree(List<SysMenu> menuList,Long pid){
        //组装数据
        List<SysMenu> list = new ArrayList<>();
        Optional.ofNullable(menuList).orElse(new ArrayList<>())
                .stream()
                .filter(item -> item != null && item.getParentId().equals(pid))
                .forEach(item->{

                    SysMenu menu = new SysMenu();
                    //要求下级菜单与父级菜单字段一样，copy
                    BeanUtils.copyProperties(item,menu);

                    //查找下级，递归算法“自己查找自己
                    List<SysMenu> children = makeTree(menuList,item.getMenuId());

                    menu.setChildren(children);
                    list.add(menu);
                });
        return list;
    }

}
